﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.View;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.View
{
    public partial class View_WorkReportingDataBusiness : BaseBusiness<View_WorkReportingData>, IView_WorkReportingDataBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public View_WorkReportingDataBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<View_WorkReportingData>> GetDataListAsync(PageInput<WorkReportingSeatch> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<View_WorkReportingData>();
            var search = input.Search;
            if (!search.StartDate.IsNullOrEmpty())
            {
                DateTime StartDate = Convert.ToDateTime(search.StartDate[0]).Date;
                DateTime EndDate = Convert.ToDateTime(search.StartDate[1]).Date.AddDays(1);
                //DateTime StartDate = Convert.ToDateTime(Convert.ToDateTime(search.StartDate).Date.ToString());
                //DateTime EndDate = Convert.ToDateTime(Convert.ToDateTime(search.EndDate).Date.ToString()).AddDays(1).AddSeconds(-1);
                q = q.Where(x => StartDate <= x.ReportTime && x.ReportTime <= EndDate);
            }
            if (!search.UserNames.IsNullOrEmpty())
            {
                q = q.Where(x=>search.UserNames.Contains(x.DeptId)|| search.UserNames.Contains(x.UserId));
            }
            if (!search.ReportUserName.IsNullOrEmpty()) {
                q = q.Where(m=>m.ReportUserName.Contains(search.ReportUserName));
            }
            q = q.Where(x => _operator.Property.DeptIds.Contains(x.DeptId));
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<View_WorkReportingData> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(View_WorkReportingData data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(View_WorkReportingData data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}